<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司编码" prop="shipperCode">
        <el-input
          v-model="queryParams.shipperCode"
          placeholder="请输入物流公司编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="deliverySn">
        <el-input
          v-model="queryParams.deliverySn"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流名称" prop="deliveryName">
        <el-input
          v-model="queryParams.deliveryName"
          placeholder="请输入物流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态 0已提交等待平台审核 1平台已审核 等待用户发货/退款 2 用户已发货 3退款成功" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态 0已提交等待平台审核 1平台已审核 等待用户发货/退款 2 用户已发货 3退款成功"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后状态-0正常1用户取消2商家拒绝" prop="salesState">
        <el-input
          v-model="queryParams.salesState"
          placeholder="请输入售后状态-0正常1用户取消2商家拒绝"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入商家收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入商家手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入商家地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-table
      v-loading="loading"
      :data="storeAfterSalesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="订单号"
        align="center"
        prop="orderCode"
        show-overflow-tooltip
      />
      <!-- <el-table-column prop="consignee" label="退款人昵称" width="100px" /> -->
      <el-table-column prop="createBy" align="center" label="联系电话" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column prop="serviceType" label="服务类型">
        <template slot-scope="scope">
          <span v-if="scope.row.serviceType === 0">仅退款</span>
          <span v-if="scope.row.serviceType === 1">退货退款</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请原因"
        align="center"
        prop="reasons"
        show-overflow-tooltip
      />
      <el-table-column
        label="说明"
        align="center"
        prop="explains"
        show-overflow-tooltip
      />
      <!-- <el-table-column label="物流单号" align="center" prop="deliverySn" />
      <el-table-column label="物流名称" align="center" prop="deliveryName" /> -->
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state === 0" :style="'color: #E6A23C'"
            >等待审核</span
          >
          <span v-if="scope.row.state === 1" :style="'color: #409EFF'"
            >等待用户发货</span
          >
          <span v-if="scope.row.state === 2" :style="'color: #F56C6C'"
            >用户已发货</span
          >
          <span v-if="scope.row.state === 3" :style="'color: #909399'"
            >退款成功</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="salesState" label="售后状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.salesState === 0" :style="'color: #42b983'"
            >正常</span
          >
          <span v-if="scope.row.salesState === 1" :style="'color: #409EFF'"
            >用户已取消</span
          >
          <span v-if="scope.row.salesState === 2" :style="'color: #F56C6C'"
            >已拒绝用户</span
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="checkItem(scope.row, 0)"
            v-hasPermi="['business:storeAfterSales:edit']"
            >详情</el-button
          >
          <el-button
            size="small"
            type="success"
            v-if="scope.row.state === 0 && scope.row.salesState === 0"
            @click="checkItem(scope.row, 1)"
            >审核</el-button
          >
          <el-button
            size="small"
            type="danger"
            v-if="scope.row.state === 2"
            @click="
              rebackVisible = true;
              rebackQuery.salesId = scope.row.id;
              rebackQuery.orderCode = scope.row.orderCode;
            "
          >
            退款</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="退款" :visible.sync="rebackVisible" width="400px">
      <span>
        是否给订单号：<b :style="'color: #409EFF'">{{
          this.rebackQuery.orderCode
        }}</b>
        确认退款？
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rebackVisible = false">取 消</el-button>
        <el-button type="primary" @click="rebackItem">确 定</el-button>
      </span>
    </el-dialog>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <CheckDialog
      ref="addForm"
      :visible="addVisible"
      @checkSuccess="handleQuery"
    />
    <!-- 添加或修改售后记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="申请原因" prop="reasons">
          <el-input
            v-model="form.reasons"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="说明" prop="explains">
          <el-input
            v-model="form.explains"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="说明图片->多个用逗号分割" prop="explainImg">
          <el-input
            v-model="form.explainImg"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="物流公司编码" prop="shipperCode">
          <el-input
            v-model="form.shipperCode"
            placeholder="请输入物流公司编码"
          />
        </el-form-item>
        <el-form-item label="物流单号" prop="deliverySn">
          <el-input v-model="form.deliverySn" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="物流名称" prop="deliveryName">
          <el-input v-model="form.deliveryName" placeholder="请输入物流名称" />
        </el-form-item>
        <el-form-item
          label="状态 0已提交等待平台审核 1平台已审核 等待用户发货/退款 2 用户已发货 3退款成功"
          prop="state"
        >
          <el-input
            v-model="form.state"
            placeholder="请输入状态 0已提交等待平台审核 1平台已审核 等待用户发货/退款 2 用户已发货 3退款成功"
          />
        </el-form-item>
        <el-form-item
          label="售后状态-0正常1用户取消2商家拒绝"
          prop="salesState"
        >
          <el-input
            v-model="form.salesState"
            placeholder="请输入售后状态-0正常1用户取消2商家拒绝"
          />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="商家收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入商家收货人" />
        </el-form-item>
        <el-form-item label="商家手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入商家手机号" />
        </el-form-item>
        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入商家地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStoreAfterSales,
  getStoreAfterSales,
  delStoreAfterSales,
  addStoreAfterSales,
  updateStoreAfterSales,
  rebackMoney,
} from "@/api/business/storeAfterSales";
import CheckDialog from "./checkDialog.vue";

export default {
  name: "StoreAfterSales",
  components: { CheckDialog },
  data() {
    return {
      codeDisabled: false,
      rebackVisible: false,
      // 退款参数
      rebackQuery: {
        orderCode: "", // 订单号
        salesId: 0, // 数据的id
      },
      addVisible: false,
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后记录表格数据
      storeAfterSalesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        refundAmount: null,
        serviceType: null,
        reasons: null,
        explains: null,
        explainImg: null,
        shipperCode: null,
        deliverySn: null,
        deliveryName: null,
        state: null,
        salesState: null,
        userId: null,
        consignee: null,
        phoneNumber: null,
        address: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
        orderCode: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
        ],
        refundAmount: [
          { required: true, message: "退款金额不能为空", trigger: "blur" },
        ],
        serviceType: [
          {
            required: true,
            message: "服务类型0仅退款1退货退款不能为空",
            trigger: "change",
          },
        ],
        reasons: [
          { required: true, message: "申请原因不能为空", trigger: "blur" },
        ],
        explains: [
          { required: true, message: "说明不能为空", trigger: "blur" },
        ],
        explainImg: [
          {
            required: true,
            message: "说明图片->多个用逗号分割不能为空",
            trigger: "blur",
          },
        ],
        shipperCode: [
          { required: true, message: "物流公司编码不能为空", trigger: "blur" },
        ],
        deliverySn: [
          { required: true, message: "物流单号不能为空", trigger: "blur" },
        ],
        deliveryName: [
          { required: true, message: "物流名称不能为空", trigger: "blur" },
        ],
        state: [
          {
            required: true,
            message:
              "状态 0已提交等待平台审核 1平台已审核 等待用户发货/退款 2 用户已发货 3退款成功不能为空",
            trigger: "blur",
          },
        ],
        salesState: [
          {
            required: true,
            message: "售后状态-0正常1用户取消2商家拒绝不能为空",
            trigger: "blur",
          },
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" },
        ],
        consignee: [
          { required: true, message: "商家收货人不能为空", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "商家手机号不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "商家地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async rebackItem() {
      this.codeDisabled = true;
      var res = await rebackMoney(this.rebackQuery);
      console.log(res);
      if (res) {
        this.$message.success("退款成功！");
        this.rebackVisible = false;
        this.getList();
      } else {
        this.$message.error("退款失败！");
      }
    },
    checkItem(row, type) {
      this.$refs.addForm.checkForm = row;
      this.$refs.addForm.serviceType = row.serviceType;
      this.$refs.addForm.form.salesId = row.id;
      this.$refs.addForm.form.orderCode = row.orderCode;

      if (type === 1) {
        this.$refs.addForm.isShow = true;
      } else {
        this.$refs.addForm.isShow = false;
      }
      this.$refs.addForm.visible = true;
    },
    /** 查询售后记录列表 */
    getList() {
      this.loading = true;
      listStoreAfterSales(this.queryParams).then((response) => {
        this.storeAfterSalesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        refundAmount: null,
        serviceType: null,
        reasons: null,
        explains: null,
        explainImg: null,
        shipperCode: null,
        deliverySn: null,
        deliveryName: null,
        state: null,
        salesState: null,
        userId: null,
        consignee: null,
        phoneNumber: null,
        address: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStoreAfterSales(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreAfterSales(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreAfterSales(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除售后记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delStoreAfterSales(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/storeAfterSales/export",
        {
          ...this.queryParams,
        },
        `storeAfterSales_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
